package ru.spb.osll.myfit.server.execute;

import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
import static org.junit.Assert.assertEquals;
import java.net.URLClassLoader;

import org.easymock.EasyMockSupport;
import org.junit.Before;
import org.junit.Test;

import ru.spb.osll.myfit.fixtures.base.DummyOneLineFixture;

import fit.Fixture;

public class MyFitFixtureLoaderTest extends EasyMockSupport {
	static final String SYSTEM_LINE_SEPARATOR = System
			.getProperty("line.separator");

	private URLClassLoader classLoader;
	private MyFitFixtureLoader fixtureLoader;

	@Before
	public void setUp() {
		classLoader = createStrictMock(URLClassLoader.class);
		fixtureLoader = new MyFitFixtureLoader(classLoader);
		fixtureLoader.addPackageToFixturePath(new DummyOneLineFixture());
	}

	@Test
	public void testDisgraceThenLoad() throws Throwable {
		expect(classLoader.loadClass("fit.DummyOneLine")).andThrow(
				new ClassNotFoundException("fit.DummyOneLine"));
		expect(classLoader.loadClass("fit.DummyOneLineFixture")).andThrow(
				new ClassNotFoundException("fit.DummyOneLineFixture"));
		expect(
				classLoader
						.loadClass("ru.spb.osll.myfit.fixtures.base.DummyOneLine"))
				.andThrow(
						new ClassNotFoundException(
								"ru.spb.osll.myfit.fixtures.base.DummyOneLine"));
		Class<?> clazz = DummyOneLineFixture.class;
		classLoader
				.loadClass("ru.spb.osll.myfit.fixtures.base.DummyOneLineFixture");
		expectLastCall().andReturn(clazz);
		replayAll();
		Fixture fixture = fixtureLoader.disgraceThenLoad("dummy one line");
		verifyAll();
		assertEquals("ru.spb.osll.myfit.fixtures.base.DummyOneLineFixture",
				fixture.getClass().getName());
	}
}
